package com.wgts.wgts.mvp.view.activity.otherhomepage;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.localbean.LocalAppModleResult;
import com.wgts.wgts.tools.MoHuGlide;

import java.util.List;

/*
 *
 *
 *   data:2018/9/1714:12
 *
 *   首页导航栏分类的viewpage+recycleView的adpter
 *
 */public class ShopClassRecycleAdapter extends RecyclerView.Adapter<ShopClassRecycleAdapter.ViewHold>{
     private OnItemClickListener onItemClickListener;
     private Context context;
     private List<LocalAppModleResult> dataList;
    public ShopClassRecycleAdapter(Context context, List<LocalAppModleResult> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @NonNull
    @Override
    public ViewHold onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHold(LayoutInflater.from(context).inflate(R.layout.view_page_recycle_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHold viewHold, int i) {
        viewHold.textView.setText(dataList.get(i).name);
        if (onItemClickListener != null){
            viewHold.selectLy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = viewHold.getLayoutPosition();
                    onItemClickListener.onItemClick(viewHold.selectLy, position);
                }
            });
        }

        MoHuGlide.getInstance().glideImgsFormList(viewHold.imageView, dataList.get(i).sourceUrl, context);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public class ViewHold extends RecyclerView.ViewHolder{
        private LinearLayout selectLy;
        private ImageView imageView;
        private TextView textView;
         public ViewHold(@NonNull View itemView) {
             super(itemView);
             imageView = itemView.findViewById(R.id.shop_image);
             textView = itemView.findViewById(R.id.shop_title);
             selectLy = itemView.findViewById(R.id.shop_ly);
         }
     }
     public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
     }

     interface OnItemClickListener {
        void onItemClick(View view, int position);
     }
}
